package com.whenJay;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@WebServlet("/cookieTest")
public class CookieTest extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String name = URLEncoder.encode("张三", "UTF-8");
//        Cookie cookie = new Cookie("name","name");
//        cookie.setMaxAge(60*60*24);
//        resp.addCookie(cookie);

        Cookie[] cookies = req.getCookies();
        for(Cookie cookie : cookies){
            if("name".equals(cookie.getName())){
                String value = cookie.getValue();
                System.out.println("解码之前的值:"+value);
                String name = URLDecoder.decode(value,"UTF-8");
                System.out.println("解码之后的值："+name);
            }
        }
    }
}
